package com.ruoyi.attendance.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.course.domain.CourseRecord;
import com.ruoyi.course.service.ICourseRecordService;
import com.ruoyi.course.service.ICourseSelectionService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.attendance.domain.AttendanceInfo;
import com.ruoyi.attendance.service.IAttendanceInfoService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 考勤记录Controller
 * 
 * @author Weiney
 * @date 2025-03-01
 */
@RestController
@RequestMapping("/attendance/attendanceRecord")
public class AttendanceInfoController extends BaseController
{
    @Autowired
    private IAttendanceInfoService attendanceInfoService;
    @Autowired
    private ICourseRecordService courseRecordService;
    @Autowired
    private ICourseSelectionService courseSelectionService;

    /**
     * 查询考勤记录列表
     */
    @PreAuthorize("@ss.hasPermi('attendance:attendanceRecord:list')")
    @GetMapping("/list")
    public TableDataInfo list(AttendanceInfo attendanceInfo)
    {
        startPage();
        attendanceInfo.setUserId(getUserId());
        List<AttendanceInfo> list = attendanceInfoService.selectAttendanceInfoList(attendanceInfo);
        return getDataTable(list);
    }

    /**
     * 查询考勤记录列表进行分析
     */
    @PreAuthorize("@ss.hasPermi('attendance:attendanceRecord:analysis')")
    @GetMapping("/analysis")
    public TableDataInfo analysis(AttendanceInfo attendanceInfo)
    {
        attendanceInfo.setUserId(getUserId());
        List<AttendanceInfo> list = attendanceInfoService.selectAttendanceInfoList(attendanceInfo);
        return getDataTable(list);
    }

    /**
     * 学生查询所有选课老师发布的考勤列表
     */
    @PreAuthorize("@ss.hasPermi('attendance:attendanceRecord:student:list')")
    @GetMapping("/student/list")
    public TableDataInfo listForStudent(AttendanceInfo attendanceInfo)
    {
        startPage();
        attendanceInfo.setUserId(getUserId());
        List<AttendanceInfo> list = attendanceInfoService.selectAttendanceInfoListForStudent(attendanceInfo);
        return getDataTable(list);
    }

    /**
     * 根据courseRecordId在sign_record表中查询签到状态
     * @param courseRecordId
     * @return
     */
    @PreAuthorize("@ss.hasPermi('attendance:attendanceRecord:student:signStatus')")
    @GetMapping("/student/{courseRecordId}")
    public AjaxResult getSignStatusByCourseRecordId(@PathVariable("courseRecordId") Long courseRecordId){
        Long userId = getUserId();
        return success(attendanceInfoService.selectSignStatusByCourseRecordId(courseRecordId, userId));
    }

    /**
     * 查询是否选课
     * @param courseRecordId
     * @return
     */
    @PreAuthorize("@ss.hasPermi('course:selectCourse:isSelect')")
    @GetMapping(value = "/courseSelection/{courseRecordId}")
    public AjaxResult getCourseSelectionStatus(@PathVariable("courseRecordId") Long courseRecordId) {
        AttendanceInfo attendanceInfo = attendanceInfoService.selectAttendanceInfoByCourseRecordId(courseRecordId);
        Long userId = getUserId();
        boolean isSelected = courseSelectionService.getCourseSelectionStatus(attendanceInfo.getCourseId(), userId);
        return success(isSelected);
    }

    /**
     * 导出考勤记录列表
     */
    @PreAuthorize("@ss.hasPermi('attendance:attendanceRecord:export')")
    @Log(title = "考勤记录", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, AttendanceInfo attendanceInfo)
    {
        List<AttendanceInfo> list = attendanceInfoService.selectAttendanceInfoList(attendanceInfo);
        ExcelUtil<AttendanceInfo> util = new ExcelUtil<AttendanceInfo>(AttendanceInfo.class);
        util.exportExcel(response, list, "考勤记录数据");
    }

    /**
     * 获取考勤记录详细信息
     */
    @PreAuthorize("@ss.hasPermi('attendance:attendanceRecord:query')")
    @GetMapping(value = "/{attendanceId}")
    public AjaxResult getInfo(@PathVariable("attendanceId") Long attendanceId)
    {
        return success(attendanceInfoService.selectAttendanceInfoByAttendanceId(attendanceId));
    }

    /**
     * 获取考勤记录结束状态
     */
    @PreAuthorize("@ss.hasPermi('attendance:attendanceRecord:endType')")
    @GetMapping(value = "/endType/{courseId}")
    public AjaxResult getAttendanceInfoEndType(@PathVariable("courseId") Long courseId){

        return success(attendanceInfoService.selectAttendanceInfoEndTypeByCourseRecordId(courseId));
    }

    /**
     * 新增考勤记录
     */
    @PreAuthorize("@ss.hasPermi('attendance:attendanceRecord:add')")
    @Log(title = "考勤记录", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody AttendanceInfo attendanceInfo)
    {
        attendanceInfo.setUserId(getUserId());
        attendanceInfo.setDeptId(getDeptId());
        // 获取当前开课对象
        CourseRecord startCourseRecord = courseRecordService.getStartCourseRecordByCourseId(attendanceInfo.getCourseId());
        // 赋值当前上课的课程记录ID
        attendanceInfo.setCourseRecordId(startCourseRecord.getId());
        return toAjax(attendanceInfoService.insertAttendanceInfo(attendanceInfo));
    }

    /**
     * 修改考勤记录
     */
    @PreAuthorize("@ss.hasPermi('attendance:attendanceRecord:edit')")
    @Log(title = "考勤记录", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody AttendanceInfo attendanceInfo)
    {
        return toAjax(attendanceInfoService.updateAttendanceInfo(attendanceInfo));
    }

    /**
     * 删除考勤记录
     */
    @PreAuthorize("@ss.hasPermi('attendance:attendanceRecord:remove')")
    @Log(title = "考勤记录", businessType = BusinessType.DELETE)
	@DeleteMapping("/{attendanceIds}")
    public AjaxResult remove(@PathVariable Long[] attendanceIds)
    {
        int row = attendanceInfoService.deleteAttendanceInfoByAttendanceIds(attendanceIds);
        if(row > 0) {
            return success();
        } else {
            return error("签到结束后才可以删除考勤记录！");
        }
    }
}
